// *************************************
// * Creator:SunnyPaine
// * DateTime:2024/08/09 16:08:57
// * Description:<Description>
// * CopyRight:
// * ***********************************

using System;
using System.ComponentModel;
using System.Reflection;
using System.Windows.Input;

namespace SunnyPaine.Autofac.Extension.Mvvm.Commands
{
    internal class CommandDescriptor : PropertyDescriptor
    {
        private readonly string name;
        private readonly bool hasParameter;


        #region 公共属性，PropertyDescriptor成员
        public override Type ComponentType { get; }

        public override bool IsReadOnly
        {
            get { return true; }
        }

        public override Type PropertyType
        {
            get { return typeof(ICommand); }
        }
        #endregion


        public MethodInfo Method { get; }

        public MethodInfo CanExecuteMethod { get; }


        #region 构造方法
        public CommandDescriptor(string name, MethodInfo method, MethodInfo canExecuteMethod, Attribute[] attributes)
            : base(name, attributes)
        {
            this.Method = method;
            this.CanExecuteMethod = canExecuteMethod;
            this.hasParameter = (method.GetParameters().Length == 1);
            this.name = name;
            this.ComponentType = method.DeclaringType;
        }
        #endregion


        #region 私有方法
        private ICommand GetCommand(object component)
        {
            return ((BasicViewModel)component).GetCommand(this.Method, this.CanExecuteMethod, this.hasParameter);
        }
        #endregion
        

        #region 公共方法，PropertyDescriptor成员
        public override bool CanResetValue(object component)
        {
            return false;
        }

        public override object GetValue(object component)
        {
            return this.GetCommand(component);
        }

        public override void ResetValue(object component)
        {
            throw new NotImplementedException();
        }

        public override void SetValue(object component, object value)
        {
            throw new NotImplementedException();
        }

        public override bool ShouldSerializeValue(object component)
        {
            return false;
        }
        #endregion
    }
}